package com.weiyuwqj.api.util;

/**
 * Created by fuxingxing on 16/4/18.
 * 10进制转成34进制并且34转成10进制
 */
public class RadixHelper {

    public static void main(String[] args) {

        System.out.println(RadixHelper.num2Radix(34, 23213));

        System.out.println(RadixHelper.radix2Num(34, "l2r"));

    }

    public static String num2ThirtyFourRadix(int dec){
        return num2Radix(34,dec);
    }

    public static int thirtyFour2NumRadix(String str){
        return radix2Num(34,str);
    }

    /**
     * 数字转换成2进制
     * @param rad 进制
     * @param dec  数值
     * @return
     */
    public static String num2Radix(int rad,int dec){
        StringBuffer sb=new StringBuffer();
        int val=0;
        while(dec>rad-1){
            val=dec%rad;
            dec=dec/rad;
            sb.append(num2Char(val));
        };
        sb.append(num2Char(dec));
        return sb.reverse().toString();
    }

    /**
     * 进制字符转换成10进制
     * @param rad 进制 2，8，26，32，34
     * @param str 需要转换的字符串
     * @return
     */
    public static int radix2Num(int rad,String  str){
        StringBuffer sb=new StringBuffer(str);
        int val=0;
        for(int i=0;i<str.length();i++){
            String c=String.valueOf(str.charAt(i));
            int tens=calTens(rad,str.length()-i);
            val=val+char2Num(c)*tens;
        }
        return val;
    }

    /**
     * 进制位数相乘
     * @param rad 进制
     * @param size  位数
     * @return
     */
    public static int calTens(int rad,int size){
        int tens=1;
        for(int j=0;j<size-1;j++){
            tens=tens*rad;
        }
        return tens;
    }

    /**
     * 单个字符转换成数字
     * @param x 单个字符
     * @return
     */
    public static  int char2Num(String x){
        int num=0;
        switch(x){
            case "0":num=0;break;
            case "1":num=1;break;
            case "2":num=2;break;
            case "3":num=3;break;
            case "4":num=4;break;
            case "5":num=5;break;
            case "6":num=6;break;
            case "7":num=7;break;
            case "8":num=8;break;
            case "9":num=9;break;
            case "a":num=10;break;
            case "b":num=11;break;
            case "c":num=12;break;
            case "d":num=13;break;
            case "e":num=14;break;
            case "f":num=15;break;
            case "g":num=16;break;
            case "h":num=17;break;
            case "j":num=18;break;
            case "k":num=19;break;
            case "l":num=20;break;
            case "m":num=21;break;
            case "n":num=22;break;
            case "p":num=23;break;
            case "q":num=24;break;
            case "r":num=25;break;
            case "s":num=26;break;
            case "t":num=27;break;
            case "u":num=28;break;
            case "v":num=29;break;
            case "w":num=30;break;
            case "x":num=31;break;
            case "y":num=32;break;
            case "z":num=33;break;
        }
        return num;
    }

    /**
     * 单个数字转换成字符
     * @param x
     * @return
     */
    public static  String num2Char(int x ){
        String letter="";
        switch(x){
            case 0:letter="0";break;
            case 1:letter="1";break;
            case 2:letter="2";break;
            case 3:letter="3";break;
            case 4:letter="4";break;
            case 5:letter="5";break;
            case 6:letter="6";break;
            case 7:letter="7";break;
            case 8:letter="8";break;
            case 9:letter="9";break;
            case 10:letter="a";break;
            case 11:letter="b";break;
            case 12:letter="c";break;
            case 13:letter="d";break;
            case 14:letter="e";break;
            case 15:letter="f";break;
            case 16:letter="g";break;
            case 17:letter="h";break;
            case 18:letter="j";break;
            case 19:letter="k";break;
            case 20:letter="l";break;
            case 21:letter="m";break;
            case 22:letter="n";break;
            case 23:letter="p";break;
            case 24:letter="q";break;
            case 25:letter="r";break;
            case 26:letter="s";break;
            case 27:letter="t";break;
            case 28:letter="u";break;
            case 29:letter="v";break;
            case 30:letter="w";break;
            case 31:letter="x";break;
            case 32:letter="y";break;
            case 33:letter="z";break;
        }
        return letter;
    }

}
